<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />

<!--- this filter is order-of-operation critical.  If the program year filter is not equal to the current program year, remove all other filters --->
<cfif NOT IsDefined("client.organizations.programYears") or resetFilters>
	<cfset client.organizations.programYears = request.currentProgramYear>
<cfelseif IsDefined("filterProgramYears")>
	<cfset client.organizations.programYears = filterProgramYears>
</cfif>
<cfif client.organizations.programYears neq request.currentProgramYear>
	<cfset isFiltered = true />
	<cfset queryParams.programYears = client.organizations.programYears />
	<cfset filterInitialized = "All" />
<cfelse>
	<cfset queryParams.programYears = request.currentProgramYear />
</cfif>

<cfif NOT IsDefined("client.organizations.approved") or resetFilters>
	<cfset client.organizations.approved = "All">
<cfelseif IsDefined("filterApproved")>
	<cfset client.organizations.approved = filterApproved>
</cfif>
<cfif client.organizations.approved neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Approved = client.organizations.approved />
</cfif>

<cfif NOT IsDefined("client.organizations.archived") or resetFilters>
	<cfset client.organizations.archived = "No">
<cfelseif IsDefined("filterArchived")>
	<cfset client.organizations.archived = filterArchived>
</cfif>
<cfif client.organizations.archived eq "All">
	<cfset isFiltered = true />
<cfelseif client.organizations.archived eq "Yes">
	<cfset isFiltered = true />
	<cfset queryParams.Archived = "Yes" />
<cfelseif client.organizations.archived eq "No">
	<cfset queryParams.Archived = "No" />
</cfif>

<cfif NOT IsDefined("client.organizations.initialized") or resetFilters>
	<cfset client.organizations.initialized = "All">
<cfelseif IsDefined("filterInitialized")>
	<cfset client.organizations.initialized = filterInitialized>
</cfif>
<cfif client.organizations.initialized neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.initialized = client.organizations.initialized />
</cfif>

<cfif NOT IsDefined("client.organizations.organizationIDs") or resetFilters>
	<cfset client.organizations.organizationIDs = "All">
<cfelseif IsDefined("filterOrganizationIDs")>
	<cfset client.organizations.organizationIDs = filterOrganizationIDs>
</cfif>
<cfif client.organizations.organizationIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.organizationIDs = client.organizations.organizationIDs />
</cfif>

<cfif IsUserInRole("Super User")>
	<cfif NOT IsDefined("client.organizations.regionIDs") or resetFilters>
		<cfset client.organizations.regionIDs = "All">
	<cfelseif IsDefined("filterRegionIDs")>
		<cfset client.organizations.regionIDs = filterRegionIDs>
	</cfif>
<cfelseif IsUserInRole("Regional Admin")>
	<cfset client.organizations.regionIDs = request.authUser.regionID>
</cfif>
<cfif client.organizations.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.organizations.regionIDs />
</cfif>

<cfwddx action="cfml2wddx" input="#queryParams#" output="client.organizations.queryParamsWDDX" />

<cfscript>
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organizations = organizationGateway.query(argumentCollection = queryParams);
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	regions = storedProcedureGateway.call("getDistinctRegions");
</cfscript>

<cfoutput>

	<h3>Organizations<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>
	
	<div class="row-fluid" style="margin-bottom: 10px;">
		<div class="span12">
			<a href="#script_name#?view=editOrganization&Organization_ID=0" class="btn">New&nbsp;organization</a>
			<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			<div class="btn-group">
				<button class="btn dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<cfif organizations.RecordCount gt 0>
						<li><a href="?action=export&collection=organizations" target="_new">Export</a></li>
					<cfelse>
						<li><a href="##">None - no records available with filter settings</a></li>
					</cfif>
				</ul>
			</div>
			<span class="pull-right"><strong>Count:</strong> #organizations.RecordCount#</span>
		</div>
	</div>

	<cfform name="organizationsForm" id="organizationsForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">
		
		<div id="filter" class="alert alert-info alert-block" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<td>
						<label>Program Year</label>
						<cfselect name="filterProgramYears" size="1" multiple="false" required="yes" message="Program Year filter is required" style="width: 150px;">
							<cfloop index="y" from="2008" to="#ListFirst(request.currentProgramYear, '-')#">
								<cfset py = "#y#-" & y+1>
								<option value="#py#"<cfif client.organizations.programYears eq py> selected</cfif>>#py#</option>
							</cfloop>
						</cfselect>
					</td>
					<cfif IsUserInRole("Super User")>
						<td>
							<label>Regions</label>
							<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" style="width: 150px;">
								<option value="All"<cfif client.organizations.regionIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="regions">
								<option value="#Region_ID#"<cfif ListFind(client.organizations.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
						</td>
					</cfif>
					<td>
						<label>Initialized (PA + OAP)</label>
						<cfif client.organizations.programYears eq request.currentProgramYear>
							<cfselect name="filterInitialized" size="1" multiple="false" required="yes" message="Initialized filter is required" style="width: 150px;">
								<option value="All"<cfif client.organizations.initialized eq "All"> selected</cfif>>All</option>
								<option value="Yes"<cfif client.organizations.initialized eq "Yes"> selected</cfif>>Yes</option>
								<option value="No"<cfif client.organizations.initialized eq "No"> selected</cfif>>No</option>
							</cfselect>
						<cfelse>
							<span class="muted"><small>Not available as filtered</small></span>
						</cfif>
					</td>
					<td>
						<label>Status</label>
						<cfselect name="filterApproved" size="1" multiple="false" required="yes" message="Status filter is required" style="width: 150px;">
							<option value="All"<cfif client.organizations.approved eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.organizations.approved eq "Yes"> selected</cfif>>Approved</option>
							<option value="No"<cfif client.organizations.approved eq "No"> selected</cfif>>Not approved</option>
						</cfselect>
					</td>
					<td>
						<label>Archived</label>
						<cfselect name="filterArchived" size="1" multiple="false" required="yes" message="Archived is required" style="width: 150px;">
							<option value="All"<cfif client.organizations.archived eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.organizations.archived eq "Yes"> selected</cfif>>Yes</option>
							<option value="No"<cfif client.organizations.archived eq "No"> selected</cfif>>No</option>
						</cfselect>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?view=organizations&resetFilters=true" class="btn btn-info">Clear</a>
					</td>
				</tr>
			</table>
		</div>

		<cfif organizations.RecordCount gt 0>

			<table id="organizationsList" style="display: none; width: 100%">
				<thead>
					<tr>
						<th>ID</th>
						<th>Organization</th>
						<cfif IsUserInRole("Super User")>
							<th>Region</th>
						</cfif>
						<th>City</th>
						<th>County</th>
						<cfif client.organizations.programYears eq request.currentProgramYear>
							<th>Initialized</th>
						</cfif>
						<th>Users</th>
						<th>Workshops</th>
						<th>Orders</th>
						<th>Exit Surveys</th>
						<th>Status</th>
					</tr>
				</thead>
				<tbody>
					<cfloop query="organizations">
						<tr>
							<td><a href="#script_name#?view=editOrganization&Organization_ID=#Organization_ID#">#Organization_ID#</a></td>
							<td>#Organization#</td>
							<cfif IsUserInRole("Super User")>
								<td>#Region_ID#</td>
							</cfif>
							<td>#City#</td>
							<td>#County#</td>
							<cfif client.organizations.programYears eq request.currentProgramYear>
								<td><cfif pa gt 0 and oap gt 0>Yes<cfelse><font color="red">No</font></cfif></td>
							</cfif>
							<!--- <td><cfif pa gt 0>Yes<cfelse><font color="red">No</font></cfif></td>
							<td><cfif oap gt 0>Yes<cfelse><font color="red">No</font></cfif></td> --->
							<td>#User_Count#</td>
							<td>#Workshop_Count#</td>
							<td>#Order_Count#</td>
							<td>#Student_Count#</td>
							<td>
								<cfif Deleted>
									<font color="red">Deleted</font>
								<cfelseif Archived>
									<font color="silver">Archived</font>
								<cfelseif NOT Approved>
									<font color="orange">Not Approved</font>
								<cfelseif Approved>
									Approved
								</cfif>
							</td>
						</tr>
				</cfloop>
				</tbody>
			</table>
			
			<script type="text/javascript">
				$(function() {
					$("##organizationsList").kendoGrid({
						sortable: {
							mode: "single",
							allowUnsort: false
						},				
						scrollable: true	
					})
					$("##organizationsList").show();
				})
			</script>
			
		<cfelse>
		
			<div class="alert alert-info notFound">No organizations</div>
	
			<script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
				});
			// -->
			</script>
		
		</cfif>

	</cfform>

</cfoutput>